//
//  MLISegementView.m
//  BaseProject
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "MLISegementView.h"
#import "MLISOneView.h"
#import "MLISTwoView.h"
#import "MLISThreeView.h"
@interface MLISegementView ()<UIScrollViewDelegate>
@property(nonatomic,assign)BOOL canScroll;
@property(nonatomic,strong)UIScrollView *mainView;
@property(nonatomic,strong)UIView *segementView;
@property(nonatomic,strong)NSMutableArray<UILabel *> *itemsArray;
@property(nonatomic,strong)UIView *moveView;
@property(nonatomic,strong)UILabel *preLab;
@property(nonatomic,strong)MLISOneView *oneView;
@property(nonatomic,strong)MLISTwoView *twoView;
@property(nonatomic,strong)MLISThreeView *threeView;
@property(nonatomic,strong)NSMutableArray<UIView *> *bigViewsArray;
@end

@implementation MLISegementView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initializationScrollProperty];
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)setParentVC:(BaseVC *)parentVC
{
    _parentVC = parentVC;
    self.twoView.parentVC = parentVC;
    self.threeView.parentVC = parentVC;
}
- (void)setViewDidAppear:(BOOL)viewDidAppear
{
    _viewDidAppear = viewDidAppear;
    _twoView.viewDidAppear = viewDidAppear;
}
#pragma mark - 主视图
- (void)createProperty
{
    
}
- (void)createUI
{
    [self createSegementViews];
    self.oneView.top = 19*ADAPTER_WIDTH;
    self.twoView.top = self.oneView.bottom + 25*ADAPTER_WIDTH;
    self.threeView.top = self.twoView.bottom + 25*ADAPTER_WIDTH;
    self.mainView.contentSize = CGSizeMake(0, MAX(self.mainView.height + 1, self.threeView.bottom + 10*ADAPTER_WIDTH));
    //追加大数组
    [self.bigViewsArray addObject:self.oneView];
    [self.bigViewsArray addObject:self.twoView];
    [self.bigViewsArray addObject:self.threeView];
}
- (void)createSegementViews
{
    CGFloat itemMargin = (self.segementView.width - 300*ADAPTER_WIDTH) / 6,orinX = itemMargin;
    NSArray *dataSource = @[@"产品特点",@"理赔流程",@"常见问题"];
    for (NSInteger index = 0; index < dataSource.count; index ++) {
        UILabel *lab = [[UILabel alloc]init];
        [self.segementView addSubview:lab];
        lab.font = kFontRegular14;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.frame = CGRectMake(orinX, 4*ADAPTER_WIDTH, 100*ADAPTER_WIDTH, 32*ADAPTER_WIDTH);
        if (index == 0) {
            lab.textColor = rgba(255, 66, 66, 1);
            [self.segementView insertSubview:self.moveView atIndex:0];
            self.moveView.center = lab.center;
            self.preLab = lab;
        } else {
            lab.textColor = rgba(255, 255, 255, 1);
        }
        lab.text = dataSource[index];
        
        orinX = lab.right + itemMargin * 2;
        //追加到数组
        [self.itemsArray addObject:lab];
        //添加手势
        lab.tag = index;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItemLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
    }
}
#pragma mark - click
- (void)clickItemLab:(UITapGestureRecognizer *)sender
{
    UILabel *lab = (UILabel *)sender.view;
    if (lab == self.preLab) {
        return;
    }
    self.preLab.textColor = rgba(255, 255, 255, 1);
    self.preLab = lab;
    self.preLab.textColor = rgba(255, 66, 66, 1);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.moveView.center = self.preLab.center;
    }];
    //内容基本超过父视图 直接使用偏移量
    [self.mainView setContentOffset:CGPointMake(0, self.bigViewsArray[lab.tag].top) animated:NO];
    
    if (!self.canScroll) {
        !self.callBackNativeJumpBlock ?: self.callBackNativeJumpBlock ();
    }
}
#pragma mark - 滑动模块
- (void)initializationScrollProperty
{
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(arriveTop:) name:@"arriveTop" object:nil];
}
- (void)arriveTop:(NSNotification *)notification
{
    self.canScroll = YES;
    //优化一波
    if (self.mainView.contentOffset.y == 0.0f && self.mainView.isDragging == NO) {
        [self.mainView setContentOffset:CGPointMake(0, 2.0f) animated:YES];
    }
    if (self.preLab.tag != 0) {
        //内容基本超过父视图 直接使用偏移量
        [self.mainView setContentOffset:CGPointMake(0, self.bigViewsArray[self.preLab.tag].top) animated:NO];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.canScroll == NO) {
        [scrollView setContentOffset:CGPointZero];
        self.mainView.showsVerticalScrollIndicator = NO;
    } else {
        if (scrollView.contentOffset.y < 0) {
            self.canScroll = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"leaveTop" object:nil];
        }
        self.mainView.showsVerticalScrollIndicator = YES;
    }
    //处理业务
    if (self.canScroll) {
        NSInteger scrollIndex = 100;
        for (NSInteger index = 0; index < self.bigViewsArray.count; index ++) {
            UIView *view = self.bigViewsArray[index];
            if (scrollView.contentOffset.y < view.bottom && scrollView.contentOffset.y > view.top) {
                scrollIndex = index;
                break;
            }
        }
        if (scrollIndex < 3) {
            UILabel *lab = self.itemsArray[scrollIndex];
            if (lab != self.preLab) {
                self.preLab.textColor = rgba(255, 255, 255, 1);
                self.preLab = lab;
                self.preLab.textColor = rgba(255, 66, 66, 1);
                [UIView animateWithDuration:0.3 animations:^{
                    self.moveView.center = self.preLab.center;
                }];
            }
        }
    }
}
#pragma mark - lazy懒加载
- (UIScrollView *)mainView
{
    if (!_mainView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.segementView.bottom + 10*ADAPTER_WIDTH, self.width, self.height - self.segementView.bottom - 10*ADAPTER_WIDTH)];
        [self addSubview:view];
        view.delegate = self;
        view.showsVerticalScrollIndicator = NO;
        _mainView = view;
    }
    return _mainView;
}
- (UIView *)segementView
{
    if (!_segementView) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(17*ADAPTER_WIDTH, 10*ADAPTER_WIDTH, self.width - 34*ADAPTER_WIDTH, 40*ADAPTER_WIDTH)];
        [self addSubview:view];
        view.layer.cornerRadius = 20*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        view.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:view.size andWithColors:@[(__bridge id)rgba(251, 175, 153, 1).CGColor,(__bridge id)rgba(252, 128, 94, 1).CGColor]];
        _segementView = view;
    }
    return _segementView;
}
- (NSMutableArray<UILabel *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (UIView *)moveView
{
    if (!_moveView) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100*ADAPTER_WIDTH, 32*ADAPTER_WIDTH)];
        view.layer.cornerRadius = 16*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        view.backgroundColor = WhiteColor;
        _moveView = view;
    }
    return _moveView;
}
- (MLISOneView *)oneView
{
    if (!_oneView) {
        MLISOneView *view = [[MLISOneView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        [self.mainView addSubview:view];
        _oneView = view;
    }
    return _oneView;
}
- (MLISTwoView *)twoView
{
    if (!_twoView) {
        MLISTwoView *view = [[MLISTwoView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        [self.mainView addSubview:view];
        _twoView = view;
    }
    return _twoView;
}
- (MLISThreeView *)threeView
{
    if (!_threeView) {
        MLISThreeView *view = [[MLISThreeView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        [self.mainView addSubview:view];
        WK(weakSelf)
        [view setCallBackChangeHeightBlock:^{
            weakSelf.mainView.contentSize = CGSizeMake(0, MAX(weakSelf.mainView.height + 1, weakSelf.threeView.bottom + 10*ADAPTER_WIDTH));
        }];
        _threeView = view;
    }
    return _threeView;
}
- (NSMutableArray<UIView *> *)bigViewsArray
{
    if (!_bigViewsArray) {
        _bigViewsArray = [NSMutableArray array];
    }
    return _bigViewsArray;
}
@end
